//
//  DownloadContractViewController.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/24.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "DownloadContractViewController.h"
#import "DownloadContractViewModel.h"
#import "GLGWPopTosView.h"
#import <UMShare/UMShare.h>

@interface DownloadContractViewController ()<MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) DownloadContractViewModel *viewModel;

@end

@implementation DownloadContractViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[DownloadContractViewModel alloc] initWithSuperView:self.view];
    kWeakSelf(self);
    self.viewModel.BlockBackBtn = ^{
        [weakself backVc];
    };
    self.viewModel.BlockSendEmail = ^(NSString *email) {
        [weakself sendEmail:email];
    };
    self.viewModel.BlockShareWX = ^(UIImage *img) {
        [weakself shareImageAndTextToPlatformType:UMSocialPlatformType_WechatSession img:img];
    };
}

- (void)backVc
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendEmail:(NSString *)emailStr
{
    if ([MFMailComposeViewController canSendMail]) {
        // 用户已设置邮件账户
        // 邮件服务器
        MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
        // 设置邮件代理
        [mailCompose setMailComposeDelegate:self];
        
        // 设置邮件主题
        [mailCompose setSubject:@"我的合同"];
        
        // 设置收件人
        [mailCompose setToRecipients:@[emailStr]];
        // 设置抄送人
        [mailCompose setCcRecipients:@[emailStr]];
        //         设置密抄送
        //        [mailCompose setBccRecipients:@[@"1780575208@qq.com"]];
        
        /**
         *  设置邮件的正文内容
         */
        NSString *emailContent = @"我是邮件内容";
        // 是否为HTML格式
        [mailCompose setMessageBody:emailContent isHTML:NO];
        // 如使用HTML格式，则为以下代码
        //    [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World！</p></body></html>" isHTML:YES];
        
        /**
         *  添加附件
         */
        UIImage *image = [UIImage imageNamed:@"icon_daizhifu"];
        NSData *imageData = UIImagePNGRepresentation(image);
        [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"image.png"];
        
        //        NSString *file = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"pdf"];
        //        NSData *pdf = [NSData dataWithContentsOfFile:file];
        //        [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"file"];
        
        [self presentViewController:mailCompose animated:YES completion:nil];
        
    } else {
        DLog(@"请先设置登录邮箱号");
        NSMutableString *mailUrl = [[NSMutableString alloc] init];
        //添加收件人
        NSArray *toRecipients = [NSArray arrayWithObject:emailStr];
        [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
        //添加抄送
        NSArray *ccRecipients = [NSArray arrayWithObjects:emailStr, nil];
        [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
        //添加密送
        NSArray *bccRecipients = [NSArray arrayWithObjects:emailStr, nil];
        [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
        //添加主题
        [mailUrl appendString:@"&subject=我的合同"];
        //添加邮件内容
        [mailUrl appendString:@"&body=<b>我的合同!</b>"];
        NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];

//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto://%@",emailStr]]];
    }

}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:nil];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled: {
            msg = @"用户取消编辑邮件";
            GLGWPopTosView *p_View = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Failure];
            p_View.tosTitle = @"发送失败！";
        }
            break;
        case MFMailComposeResultSaved: {
            msg = @"用户成功保存邮件";
            GLGWPopTosView *p_View2 = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Success];
            p_View2.tosTitle = @"保存成功!";
        }
            break;
        case MFMailComposeResultSent: {
            msg = @"用户点击发送，将邮件放到队列中，还没发送";
            GLGWPopTosView *p_View3 = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Failure];
            p_View3.tosTitle = @"发送失败!";
        }
            break;
        case MFMailComposeResultFailed: {
            msg = @"用户试图保存或者发送邮件失败";
            GLGWPopTosView *p_View3 = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Failure];
            p_View3.tosTitle = @"发送失败!";
        }
            break;
        default:
            msg = @"";
            break;
    }
//    [self alertWithMessage:msg];
}

- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType img:(UIImage *)img
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //设置文本
    messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用，快速武装App。";
    
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    shareObject.thumbImage = img;
    [shareObject setShareImage:img];

//    [shareObject setShareImage:@"https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=dog%20icon&hs=2&pn=83&spn=0&di=130906367020&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=3799961250%2C1734615991&os=2569125717%2C1721404317&simid=3453753772%2C377853677&adpicid=0&lpn=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=dog%20icon&objurl=http%3A%2F%2Fpic.666pic.com%2Fthumbs2%2F1423984%2F69255823%2Fapi_thumb_450.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bmmmrtv_z%26e3Bv54AzdH3Ffijytg2AzdH3Fladb9mmb_z%26e3Bip4s&gsm=3c&islist=&querylist="];
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        } else {
//            NSLog(@"response data is %@",data);
            GLGWPopTosView *p_View = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Success];
            p_View.tosTitle = @"发送成功!";
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

@end
